package de.baumann.browser.present

import de.baumann.browser.api.OdinObserver
import de.baumann.browser.api.net.vo.QiNiuToken
import de.baumann.browser.base.BasePresenter
import de.baumann.browser.iview.ISendPackView
import de.baumann.browser.model.PackModel

class SendPackPresenter : BasePresenter<ISendPackView>() {
    private val packModel = PackModel()

    fun sendPack() {
        packModel.sendPack(view.getIsUpload(),
                view.getPackDesc(),
                view.getPackImageUrls(),
                view.getPackAmount(),
                view.getPackCount(),
                view.getPackAdUrl(),
                view.getPackAdLocation())
                .subscribe(object : OdinObserver<Any>(view) {
                    override fun onSuccess(data: Any?) {
                        if (view != null)
                            view.sendPackSuccess()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        if (code == 1001 && view != null) {
                            getToken()
                        }
                    }
                })
    }

    fun getToken() {
        this.packModel.qiNiuToken
                .subscribe(object : OdinObserver<QiNiuToken>() {
                    override fun onSuccess(data: QiNiuToken?) {
                        if (data != null && view != null) {
                            view.uploadImage(data.ossToken)
                        }
                    }
                })
    }


}